package net.huashitong.Indicators.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import com.sirdc.modules.utils.date.DateUtils;

public class DateForIndUtils {
	/**
	 * 获取n个月后的时间
	 * @return yyyyMMdd
	 * @throws ParseException 
	 */
	public static String getNextMonthDate(String strDate, int n) throws ParseException
	{
		Date date = new SimpleDateFormat("yyyyMM").parse(strDate);
				
		Calendar calendar = Calendar.getInstance();//创建实例

		calendar.setTime(date);

		calendar.add(Calendar.MONTH, n);
		
		date = calendar.getTime();
		
		String curdate = DateUtils.formatDate(date, "yyyyMM");
		
		
		return curdate;
	}
	/**
	 * 获取几年后的时间
	 * @return yyyyMMdd
	 * @throws ParseException 
	 */
	public static String getNextYearDate(String strDate, int n) throws ParseException
	{
		Date date = new SimpleDateFormat("yyyy").parse(strDate);
				
		Calendar calendar = Calendar.getInstance();//创建实例

		calendar.setTime(date);

		calendar.add(Calendar.YEAR, n);
		
		date = calendar.getTime();
		
		String curdate = DateUtils.formatDate(date, "yyyy");
		
		
		return curdate;
	}
	
	/**
	 * 获取n个月后的时间
	 * @return yyyyMMdd
	 * @throws ParseException 
	 */
	public static String getNextMonthValueDate(String strDate, int n) throws ParseException
	{
		Date date = new SimpleDateFormat("yyyyMM-MM").parse(strDate);
				
		Calendar calendar = Calendar.getInstance();//创建实例

		calendar.setTime(date);

		calendar.add(Calendar.MONTH, n);
		
		date = calendar.getTime();
		
		String curdate = DateUtils.formatDate(date, "yyyyMM-MM");
		
		curdate = curdate.substring(0, 4)+"01"+curdate.substring(6);
		return curdate;
	}
	/**
	 * 
	 * @param date
	 * @return
	 */
	public static String getMonth(String date){
		if(date.contains("01-")){
			return date;
		}
		date = date.substring(0,4)+"01-"+date.substring(4);
		return date;
	}
	
	/**
	 * 获取当前日期
	 * @param format
	 * @return
	 */
	public static String getCurDate(String format){
		SimpleDateFormat df = new SimpleDateFormat(format);
		String date = df.format(new Date());
		return date;
	}
	public static String getCMonth(String date) {
		if(date.contains("01-")){
			return date.replace("01-", "");
		}
		
		return date;
	}

	/**
	 * 字符串转化为标准时间
	 *
	 * @param text
	 * @param formate
	 * @return
	 */
	public static Date toDate(String text, String... formate) {
		SimpleDateFormat sf = new SimpleDateFormat(formate[0].toString());
		try {
			return sf.parse(text);
		} catch (ParseException e) {
			e.printStackTrace();
			return null;
		}
	}
}
